package com.activenetwork.smlite.testcase.events.eventListSection;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.schema.testcase.events.eventlistsection.eventlistsection006.EventListSection006DataType;
import com.activenetwork.smlite.business.EventBusiness;
import com.activenetwork.smlite.page.event.EventDetailView.EventSummarySubview;
import com.activenetwork.smlite.page.generic.ModelPage;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.jaxb.ScriptDataBindHelper;
import com.activenetwork.util.verification.gson.FailurePoint;

public class EventListSection006 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(EventListSection006.class);
	private EventListSection006DataType data;
	
	@BeforeClass(alwaysRun = true)
	public void dataSetup(){
		try{
			ScriptDataBindHelper helper = new ScriptDataBindHelper(EventListSection006.class);
			data = helper.bind(EventListSection006DataType.class);
		}catch(IOException e){
			throw new RuntimeException("Unable to bind the script data for " + this.getClass().getName());
		}
	}
	
	@Test
	public void Verify_Event_Name_link_takes_user_to_Events_Summary_page_correctly(){
		loginBusiness.login();
		
		// Delete the specific event and add it back
		EventBusiness eventBusiness = new EventBusiness();
		try{
			eventBusiness.deleteEvent(data.getEvent().getBasic().getTitle(), data.getEvent());
		}catch(RuntimeException e){
			logger.info("No need to delete the event since it is not exist");
		}
		
		eventBusiness.addEvent(data.getEvent());
		PageLoader.load(ModelPage.class).events();
		
		// Verify the specific event can be searched out
		eventBusiness.searchEvent(data.getEvent().getBasic().getTitle(), data.getEvent());
		EventSummarySubview eventSummarySubview = PageLoader.load(EventSummarySubview.class);
		String eventSummaryTitle = eventSummarySubview.getEventTitle();
		
		if(!StringUtils.equalsIgnoreCase(eventSummaryTitle, data.getEvent().getBasic().getTitle())){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(EventListSection006.class.getName(), 
					"Verify event can be retrieved and summary displayed", 
					"Retrieve existing event failed"));
		}
		
		loginBusiness.logout();
	}
}
